package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.pawnbackgroundsystem.commons.JwtUtils;
import com.wnxy.pawnbackgroundsystem.commons.ServiceImplUtil;
import com.wnxy.pawnbackgroundsystem.dao.GoodsMapper;
import com.wnxy.pawnbackgroundsystem.dao.IdentifyMapper;
import com.wnxy.pawnbackgroundsystem.entity.Goods;
import com.wnxy.pawnbackgroundsystem.entity.Identify;
import com.wnxy.pawnbackgroundsystem.entity.User;
import com.wnxy.pawnbackgroundsystem.param.identifyParam.IdentifyParam;
import com.wnxy.pawnbackgroundsystem.service.IIdentifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 鉴定历史表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-10-17
 */
@Service
@Slf4j
public class IdentifyServiceImpl extends ServiceImpl<IdentifyMapper, Identify> implements IIdentifyService {

    @Resource
    private IdentifyMapper identifyMapper;

    @Resource
    private GoodsMapper goodsMapper;


    @Override
    public IPage<Identify> findAllidentifyByPage(Integer goodsId, Integer pageSize, Integer current) {
        log.info("查询鉴定历史业务层：查询所有商品信息接受到的数据goodsid{},当前页{}，每页{}条,",goodsId,pageSize,current);
        Page page = new Page(current,pageSize);

        QueryWrapper<Identify> qw = new QueryWrapper<>();
        qw.eq("goods_id",goodsId);

        IPage<Identify> identifyPage = identifyMapper.selectPage(page,qw);

        log.info("查询鉴定历史业务层传回控制层的参数：{}",identifyPage.getRecords());

        return identifyPage;
    }

    @Override
    public Integer identifyTure(IdentifyParam identifyParam) {
        log.info("鉴定为真品，参数identifyparam:{}",identifyParam);

        Identify identify = new Identify();
        //获取当前登录用户名
        User user = ServiceImplUtil.getJwtUtilUser(JwtUtils.AUTH_TOKEN_NAME);
        log.info("从jwt中拿去登录人的信息user:{}",user);
        identify.setGoodsDefinePerson(user.getUserName());
        identify.setGoodsDefineTime(System.currentTimeMillis());

        identify.setIdentifyDegree(identifyParam.getIdentifyDegree());
        identify.setIdentifyRemark(identifyParam.getIdentifyRemark());
        identify.setIdentifyType(identifyParam.getIdentifyType());
        identify.setGoodsId(identifyParam.getGoodsId());
        int identifyTure = identifyMapper.insert(identify);

        Goods goods = new Goods();
        goods.setGoodsId(identifyParam.getGoodsId());
        goods.setGoodsStatus("已鉴定");
        int i = goodsMapper.updateById(goods);
        return identifyTure;

    }

    @Override
    public Integer identifyFalse(IdentifyParam identifyParam) {
        log.info("鉴定为假货，参数identifyparam:{}",identifyParam);

        Identify identify = new Identify();
        //获取当前登录用户名
        User user = ServiceImplUtil.getJwtUtilUser(JwtUtils.AUTH_TOKEN_NAME);
        log.info("从jwt中拿去登录人的信息user:{}",user);
        identify.setGoodsDefinePerson(user.getUserName());
        identify.setGoodsDefineTime(System.currentTimeMillis());

        identify.setIdentifyDegree(identifyParam.getIdentifyDegree());
        identify.setIdentifyRemark(identifyParam.getIdentifyRemark());
        identify.setIdentifyType("假货");
        identify.setGoodsId(identifyParam.getGoodsId());
        int identifyFalse = identifyMapper.insert(identify);

        Goods goods = new Goods();
        goods.setGoodsId(identifyParam.getGoodsId());
        goods.setGoodsStatus("已鉴定");
        int i = goodsMapper.updateById(goods);
        return identifyFalse;



    }

    @Override
    public Integer identifyInvalid(IdentifyParam identifyParam) {
        log.info("资料不全，鉴定不了id:{}",identifyParam.getGoodsId());
        Identify identify = new Identify();
        //获取当前登录用户名
        User user = ServiceImplUtil.getJwtUtilUser(JwtUtils.AUTH_TOKEN_NAME);
        log.info("从jwt中拿去登录人的信息user:{}",user);
        identify.setGoodsDefinePerson(user.getUserName());
        identify.setGoodsDefineTime(System.currentTimeMillis());


        identify.setIdentifyRemark(identifyParam.getIdentifyRemark());
        int insert = identifyMapper.insert(identify);
        Goods goods = new Goods();
        goods.setGoodsId(identifyParam.getGoodsId());
        goods.setGoodsStatus("无效");
        int i = goodsMapper.updateById(goods);

        return insert;
    }
}
